<?php
    // Author: Albert Wu
    // Filename: store.php
    // Assignment: Palo Alto Bicycle Club
    // Date Created: 6/21/09
    // PHP for displaying Amazon product listing

   getListing($_GET["page"]);

    // Sends request query to Amazon for product listing
    function getListing($page) {
        $request = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=0F9MP0M1VEMNS58R1402";
        $request .= "&Operation=ItemSearch&ResponseGroup=Medium,OfferFull,VariationSummary&SearchIndex=SportingGoods&Keywords=Road,Bicycle&ItemPage=$page";
        
        printListing($page, simplexml_load_string( file_get_contents($request)));
    }
    
    // Displays product listing
    function printListing($page, $listing) {
        printNav($page, $listing);

        echo "<table>";

        foreach ($listing->Items->Item as $current) {
            if (isset($current->MediumImage->URL)) {
                echo "<tr><td><img src='", $current->MediumImage->URL;
                echo "' /></td><td>", $current->ItemAttributes->Title;

                if (isset($current->Offers->Offer->OfferListing->Price->FormattedPrice))
                    echo "<br />Price: ", $current->Offers->Offer->OfferListing->Price->FormattedPrice;
                else
                    if (isset($current->VariationSummary->LowestPrice->FormattedPrice))
                        echo "<br />Price: ", $current->VariationSummary->LowestPrice->FormattedPrice;
                    else
                        if (isset($current->OfferSummary->LowestNewPrice->FormattedPrice))
                            echo "<br />Price: ", $current->OfferSummary->LowestNewPrice->FormattedPrice;
                        else
                            echo "<br />Price: ", $current->OfferSummary->LowestUsedPrice->FormattedPrice;

                echo "<br /><a href='", $current->DetailPageURL, "' target='_blank'>See Details</a></td></tr>";
            }
        }

        echo "</table>";
    }
    
    // Displays listing nagivation
    function printNav($page, $listing) {
        $pages = ($listing->Items->TotalPages < 400) ? $listing->Items->TotalPages : 400;

        if ($page <= 10) {
            $start = 1;
            $end = 10;
        }  
        else 
            if ($page >= $pages - 10) {
                $start = $pages - 10;
                $end = $pages;
            }
            else {
                $start = $page - 5;
                $end = $page + 5;
            }

        echo "<div id='nav'>";

        if ($page > 1) {
            echo "<a onclick='getStore(1);'><<</a>&nbsp;";
            echo "<a onclick='getStore($page - 1);'><</a>&nbsp;";
        }
        
        for ($i = $start; $i <= $end; $i++) {
            if ($i != $page) 
                echo "&nbsp;<a  onclick='getStore($i);'>[$i]</a>&nbsp;";
            else  
                echo "&nbsp;[$i]&nbsp;";		
        }
        
        if ($page < $pages) {
            echo "&nbsp;<a onclick='getStore($page + 1);'>></a>&nbsp;";
            echo "<a onclick='getStore($pages);'>>></a>";
        }

        echo "</div>";
    }
?>